package com.mes.inventory.service;

import java.util.Collection;
import java.util.Date;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.DataResolver;
import com.bstek.dorado.data.entity.EntityUtils;
import com.bstek.dorado.data.provider.Page;
import com.mes.common.service.BasicService;
import com.mes.common.util.IdentityGenerator;
import com.mes.common.util.SysUtil;
import com.mes.inventory.dao.InvIODetailDao;
import com.mes.inventory.dao.MaterialDao;
import com.mes.inventory.dao.iface.IInvlnfoDao;
import com.mes.inventory.domain.InvInfo;
import com.mes.inventory.domain.Material;
@Service
public class InvInfoService extends BasicService<InvInfo>{
	@Autowired
	private IInvlnfoDao invlnfoDao;
	@Autowired
	private MaterialDao materialDao;
	@Autowired
	private InvIODetailDao invIODetailDao;
	
	//条件查询
	@DataProvider
	public void getInfoByConditions(Page<InvInfo> page,Map<String,Object> parameter) throws Exception{
		if (parameter != null && !parameter.equals("") ){
			invlnfoDao.getInvInfoByCondition(page,parameter);
		}
	}
	//柜位移动
	@DataResolver
	public void moveInvInfo(Collection<InvInfo> invInfos,String warehouseUid) throws Exception{
		for (InvInfo invInfo : invInfos) {
			
			String newBinId = EntityUtils.getValue(invInfo, "newBinId");
			Integer moveNum = EntityUtils.getInt(invInfo, "moveNum");
			float moveNumf = (float)moveNum;
			String materialUid = invInfo.getMaterialUid();
			String oldBinId = invInfo.getWarebinUid();
			InvInfo info = invlnfoDao.getInvInfo(materialUid, warehouseUid, newBinId);
			if (newBinId != null && newBinId != oldBinId) {
				if (null != info) {
					info.setInvQty(info.getInvQty()+moveNumf);
					invlnfoDao.update(info);
				}else {
					InvInfo in = new InvInfo();
					in.setWarebinUid(newBinId);
					in.setMaterialUid(materialUid);
					in.setWarehouseUid(warehouseUid);
					in.setPartName(invInfo.getPartName());
					in.setPartDrawingId(invInfo.getPartDrawingId());
					in.setBatchNum(invInfo.getBatchNum());
					in.setInvQty(moveNumf);
					in.setSn(invInfo.getSn());
					invlnfoDao.add(in);
				}
				float resultNum = invInfo.getInvQty()-moveNumf;
				if(resultNum>0){
					invInfo.setInvQty(invInfo.getInvQty()-moveNumf);
					invlnfoDao.update(invInfo);
				}else if(resultNum == 0){
					invlnfoDao.delete(invInfo);
				}else{
					throw new Exception("库存不足!");
				}
			}
		}
	}
	//查询各个库房是否已经初始化
		@DataProvider
	    public void getInvInfo(Page<InvInfo> page, Map<String, Object> parameter) throws Exception{
	    	if (parameter != null && !parameter.equals("") ){
	    		invlnfoDao.getInvInfo(page, parameter);		
	    	}
		}
		//根据柜位模糊查询
		@DataProvider
	    public void getInvInfoByCondition(Page<InvInfo> page, Map<String, Object> params) throws Exception{
	    	if (params != null && !params.equals("") ){
	    		try {
	    			invlnfoDao.getInvInfos(page, params);
	    		} catch (Exception e) {
	    			e.printStackTrace();
	    		}
	    	}
	    }
		
		
	
		//添加
		@DataResolver
		public void initInvInfo(Map<String, Collection> dataItems) throws Exception{
				for (String key : dataItems.keySet()){
					Collection<InvInfo> invInfos = dataItems.get(key);
					for (InvInfo invInfo : invInfos) {
						if (invInfo.getUniqueId()==null) {
							invInfo.setUniqueId(IdentityGenerator.GetIdentityUUID());
						}
						if (invInfo.getMaterialUid()==null) {
							invInfo.setMaterialUid(IdentityGenerator.GetIdentityUUID());
						}
						//获取当前要添加的InvInfo属性{
						String partUid = invInfo.getPartUid();
						String batchNum = invInfo.getBatchNum();
						String sn = invInfo.getSn();
						//通过这三个属性去materia中去查
						Material material1 = materialDao.getMaterialByPartUidBatchNumSn(partUid,
								batchNum, sn);
						if (material1 == null) {
							Material material = new Material();
							material.setUuid(SysUtil.getUUID());
							material.setPartUid(invInfo.getPartUid());
							material.setPartDrawingId(invInfo.getPartDrawingId());
							material.setPartName(invInfo.getPartName());
							material.setPartVersionId(invInfo.getPartVersionId());
							material.setPartDescription(invInfo.getPartDescription());
							material.setMaterialType(invInfo.getMaterialType());
							material.setBatchNum(invInfo.getBatchNum());
							material.setSn(invInfo.getSn());
							material.setCheckupDate(new Date());
							material.setMaterialQty(invInfo.getInvQty());
							materialDao.add(material);
							
							invInfo.setMaterialUid(material.getUuid());
						}
						this.saveAll(invInfos);
						}
					}	
				
		}
}
